package com.example.teacher

/**
 * 添加考勤
 */
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.teacher.data.Course
import com.example.teacher.databinding.ActivityAddBinding
import com.example.teacher.http.OkHttp
import com.example.teacher.util.JsonUtil

class AddActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAddBinding
    var chooseitem = arrayOf("数字签到", "二维码签到")
    var choose = 0
    var chooseCourseId=0
    var QrData="@"
    var selectData=""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_add)

        binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                choose = position
//                Toast.makeText(this@AddActivity, choose.toString(), Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                Toast.makeText(this@AddActivity, "没有选择签到方式", Toast.LENGTH_SHORT).show()
            }


        }

        binding.btnadd.setOnClickListener {
            if (choose == 0) {
                add_num()
            } else if (choose == 1) {
                QrData+=selectData+binding.editTextSection.text
                add_QRcode()
            }
        }

        getCourseList()

    }

    //创建二维码签到
    private fun add_QRcode() {
        val courseId = binding.editTextCourseId.text
        val courseName = binding.editTextCourseName.text
        val teacher = binding.editTextTeacher.text
        val number = binding.editTextNumber.text
        val section = binding.editTextSection.text
        val res = OkHttp.getSync(
            "${BaseUrl.URL}${BaseUrl.POST_SIGN}?" +
                    "courseId=${courseId}" +
                    "&courseName=${courseName}" +
                    "&teacher=${teacher}" +
                    "&number=${number}" +
                    "&section=${section}"
        )

        if (res.equals("1")) {
            Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show()
            val intent=Intent(this,QRCodeActivity::class.java)
            intent.putExtra("QrData",QrData)
            QrData="@"
            startActivity(intent)
        }else{
            Toast.makeText(this, "发布失败", Toast.LENGTH_SHORT).show()

        }

    }


    //创建数字签到
    private fun add_num() {
        val courseId = binding.editTextCourseId.text
        val courseName = binding.editTextCourseName.text
        val teacher = binding.editTextTeacher.text
        val number = binding.editTextNumber.text
        val section = binding.editTextSection.text
        val res = OkHttp.getSync(
            "${BaseUrl.URL}postSign?" +
                    "courseId=${courseId}" +
                    "&courseName=${courseName}" +
                    "&teacher=${teacher}" +
                    "&number=${number}" +
                    "&section=${section}"
        )
        if (res.equals("1")) {
            Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show()
            startActivity(Intent(this, GetImageActivity::class.java))
        }else{
            Toast.makeText(this, "发布失败", Toast.LENGTH_SHORT).show()
        }


    }

    fun getCourseList(){

        val resData = OkHttp.getSync("${BaseUrl.URL}${BaseUrl.GET_COURSELIST}")
        val dataParse = JsonUtil.DataParse<Course>(resData)

        val mItem= arrayListOf<String>()
        for (i in dataParse.indices){
            mItem.add(dataParse[i].courseId)
        }
        val arrayAdapter=ArrayAdapter(this,android.R.layout.simple_spinner_item,mItem)
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.spinnerCourse.adapter=arrayAdapter

        binding.spinnerCourse.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                chooseCourseId = position
                binding.editTextCourseName.text= Editable.Factory.getInstance().newEditable(dataParse[position].courseName)
                binding.editTextNumber.text=Editable.Factory.getInstance().newEditable(dataParse[position].courseNum)
                binding.editTextTeacher.text=Editable.Factory.getInstance().newEditable(dataParse[position].teacher)
                binding.editTextCourseId.text=Editable.Factory.getInstance().newEditable(dataParse[position].courseId)

                //将信息存入QrData，传到其他activity
                selectData=dataParse[position].courseId+"@"

                //Toast.makeText(this@AddActivity, choose.toString(), Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                Toast.makeText(this@AddActivity, "没有选择签到方式", Toast.LENGTH_SHORT).show()
            }



        }
    }
}